package com.woniuxy.controller;

import com.woniuxy.entity.Constants;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.ResultVO;
import com.woniuxy.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author : mafan
 * @date : 2020-11-18 20:31
 **/
@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @GetMapping("{pid}")
    public ResultVO find(@PathVariable Integer pid) {
        Product product = productService.find(pid);
        ResultVO resultVO = null;
        if (product != null) {
            resultVO = new ResultVO(Constants.OPER_SUCCESS, "获取商品成功", product);
        } else {
            resultVO = new ResultVO(Constants.OPER_FAILURE, "获取商品失败");
        }

        return resultVO;
    }

    @PostMapping("deductStock/{pid}/{number}")
    public ResultVO deductStock(@PathVariable Integer pid, @PathVariable Integer number){
        System.out.println("ProductController.deductStock");
        ResultVO resultVO = null;
        try {
            productService.deductStock(pid, number);
            resultVO = new ResultVO(Constants.OPER_SUCCESS, "扣减库存成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO(Constants.OPER_FAILURE, "扣减库存失败："+e.getMessage());
        }
        return resultVO;
    }


}
